package com.zhf.service.impl;

import com.zhf.dao.RoleMapper;
import com.zhf.domain.Role;
import com.zhf.domain.RoleMenuVo;
import com.zhf.domain.Role_menu_relation;
import com.zhf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/12/5 13:13
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAllRole(Role role) {
        List<Role> roles = roleMapper.findAllRole(role);
        return roles;
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleid) {
        List<Integer> menu = roleMapper.findMenuByRoleId(roleid);
        return menu;
    }

    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {
        //清空中间表
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        //为角色分配菜单
        List<Integer> idList = roleMenuVo.getMenuIdList();
        for (Integer integer : idList) {
            Role_menu_relation role_menu_relation = new Role_menu_relation();
            role_menu_relation.setMenuId(integer);
            role_menu_relation.setRoleId(roleMenuVo.getRoleId());

            //封装数据
            Date date = new Date();
            role_menu_relation.setCreatedTime(date);
            role_menu_relation.setUpdatedTime(date);
            role_menu_relation.setCreatedBy("system");
            role_menu_relation.setUpdatedby("system");
            roleMapper.roleContextMenu(role_menu_relation);
        }
    }

    @Override
    public void deleteRole(int id) {

        roleMapper.deleteRoleContextMenu(id);

        roleMapper.deleteRole(id);
    }
}
